package com.hollycrm.sip.proxy;

import java.util.Hashtable;
import java.util.Iterator;

public class SipMessage {
    private String request;
    
    private Hashtable<String, String> map = new Hashtable<>();
    
    private Sdp sdp = new Sdp();
    
    public SipMessage(){
        
    }
    
    public String getRequest() {
        return request;
    }

    /**
     * v=0
o=- 1304685641 950925 IN IP4 192.168.1.3
s=-
c=IN IP4 116.231.21.84
t=0 0
m=audio 10010 RTP/AVP 18 101
a=ptime:20
a=rtpmap:18 G729/8000
a=sendrecv
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
     */
    public Hashtable<String, String> paser(String message) throws Exception{
        String a[] = message.split("\r\n");
        request = a[0];
        String b = null;
        String[] data = null;
        for(int i=1;i<a.length;i++) {
            b = a[i];
            if(b.indexOf(": ") != -1 && !b.startsWith("a=")){
                data = b.split(": ");
                if(data.length > 1)
                    map.put(data[0], data[1]);
                else map.put(data[0], "");
            } else if(!b.startsWith("\r\n") && !b.equals("")){
                sdp.parse(b);
            }
        }
        return map;
    }
    
    public String getHeader(String key) {
        return map.get(key);
    }
    
    public void addHeader(String key, String value) {
        map.put(key, value);
    }
    
    public Sdp getSdp() {
        return sdp;
    }

    public String getSipMessage() {
        StringBuilder builder = new StringBuilder();
        Iterator<String> it = map.keySet().iterator();
        builder.append(request);
        while(it.hasNext()) {
            String key = it.next();
            builder.append("\r\n").append(key).append(": ").append(map.get(key));
        }
        builder.append("\r\n").append(sdp.getString());
        if(builder.substring(builder.length() -2).equals("\r\n")){
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append("\r\n\r\n");
        return builder.toString();
    }
}
